package org.infodavid.common.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IUserSessionListItem;
import org.infodavid.common.impl.model.criteria.UserSessionCriteriaImpl;
import org.infodavid.common.model.IUserSession;
import org.infodavid.common.model.UserSessionTest;
import org.infodavid.common.model.criteria.IUserSessionCriteria;

/**
 * The Class UserSessionDataServiceTest.
 */
@SuppressWarnings({
	"null"
})
public class UserSessionDataServiceTest extends
    AbstractDataServiceTest<IUserSessionListItem,IUserSession,String> {

	/**
	 * The Constructor.
	 */
	public UserSessionDataServiceTest() {
		super(new UserSessionTest(), UserSessionDataService.getInstance());
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.AbstractDataServiceTest#modify(org.infodavid.common.model.IDataObject
	 * )
	 */
	@Override
	public void modify(final IUserSession item) throws Exception {
		item.setLastIP("ModifiedIP");
	}

	/**
	 * Test get instance.
	 */
	@Test
	public final void testGetInstance() {
		Assert.assertEquals("Not a singleton", UserSessionDataService.getInstance(),
		    UserSessionDataService.getInstance());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteria() throws Exception {
		final List<IUserSessionListItem> results = new ArrayList<IUserSessionListItem>();
		final IUserSessionCriteria criteria = new UserSessionCriteriaImpl();
		final long count = ((UserSessionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingAllCriterions() throws Exception {
		final Collection<IUserSession> inserted = getTestData();
		final IUserSession session = inserted.iterator().next();
		final List<IUserSessionListItem> results = new ArrayList<IUserSessionListItem>();
		final IUserSessionCriteria criteria = new UserSessionCriteriaImpl();

		criteria.setUserKey(session.getUser().getKey());

		final long count = ((UserSessionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertTrue("Not all data has been found", 1 <= results.size());
		Assert.assertEquals("Not all data has been found", count, results.size());
		Assert.assertTrue("Wrong data found", results.contains(session));
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingGroup() throws Exception {
		final Collection<IUserSession> inserted = getTestData();
		final List<IUserSessionListItem> results = new ArrayList<IUserSessionListItem>();
		final IUserSessionCriteria criteria = new UserSessionCriteriaImpl();
		final IUserSession session = inserted.iterator().next();

		criteria.setUserKey(session.getUser().getKey());

		final long count = ((UserSessionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(count);
		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertTrue("Not all data has been found", 1 <= results.size());
		Assert.assertEquals("Not all data has been found", count, results.size());
		Assert.assertTrue("Wrong data found", results.contains(session));
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCollection() throws Exception {
		final IUserSessionCriteria criteria = new UserSessionCriteriaImpl();
		Exception exception = null;

		try {
			((UserSessionDataService)getService()).find(getSession(), null, criteria);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("No exception thrown", exception);
		Assert.assertEquals("Wrong exception thrown", IllegalArgumentException.class,
		    exception.getClass());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCriteria() throws Exception {
		final List<IUserSessionListItem> results = new ArrayList<IUserSessionListItem>();
		final long count =
		    ((UserSessionDataService)getService()).find(getSession(), results,
		        (IUserSessionCriteria)null);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingPage() throws Exception {
		final List<IUserSessionListItem> results = new ArrayList<IUserSessionListItem>();
		final IUserSessionCriteria criteria = new UserSessionCriteriaImpl();

		criteria.setPageStart(Long.valueOf(0));
		criteria.setPageSize(Integer.valueOf(5));

		final long count = ((UserSessionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", criteria.getPageSize().intValue(),
		    results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}
}
